angular.module('myApp').controller('homepage', ["$scope", "$http","$state",
    function ($scope, $http,$state) {
        var a, b;
        $http({
            method: 'post',
            url: '/homePage'
        }).then(function (data) {
            console.log(data.data);
            $scope.students = data.data.list;
            var tt=data.data.list;
            var color=["active","success","warning","danger","info"];
            for (var i=0;i<tt.length;i++){
                var op=tt[i];
                op.color=color[i%color.length];
                if(op.sex=='0'){
                    op.sexd="男";
                }
                if(op.sex=='1'){
                    op.sexd="女";
                }

            }
            $scope.id = data.data.list.id;
            $scope.thepage = data.data.pageNumber;
            <!--点击按钮把最新的当前页赋值给变量a-->
            a = data.data.pageNumber;
            $scope.totalpage = data.data.totalPage;
            <!--获取总页数赋值给变量b-->
            b = data.data.totalPage;
        });
        $scope.lastpage = function () {
            if (a == 1) {
                alert("没有上一页了");
            } else {
                $http({
                    method: 'post',
                    url: '/homePage',
                    data: {
                        pageNum: a - 1
                    }
                }).then(function (data) {
                    $scope.students = data.data.list;
                    var tt=data.data.list;
                    var color=["active","success","warning","danger","info"];
                    for (var i=0;i<tt.length;i++){
                        var op=tt[i];
                        op.color=color[i%color.length];
                        if(op.sex=='0'){
                            op.sexd="男";
                        }
                        if(op.sex=='1'){
                            op.sexd="女";
                        }
                    }
                    $scope.thepage = data.data.pageNumber;
                    <!--点击按钮把最新的当前页赋值给变量a-->
                    a = data.data.pageNumber;
                });
            }
        }
        $scope.nextpage = function () {
            if (a == b) {
                alert("已经是最后一页");
            } else {
                $http({
                    method: 'post',
                    url: '/homePage',
                    data: {
                        pageNum: a + 1
                    }
                }).then(function (data) {
                    $scope.students = data.data.list;
                    var tt=data.data.list;
                    var color=["active","success","warning","danger","info"];
                    for (var i=0;i<tt.length;i++){
                        var op=tt[i];
                        op.color=color[i%color.length];
                        if(op.sex=='0'){
                            op.sexd="男";
                        }
                        if(op.sex=='1'){
                            op.sexd="女";
                        }
                    }
                    $scope.thepage = data.data.pageNumber;
                    <!--点击按钮把最新的当前页赋值给变量a-->
                    a = data.data.pageNumber;
                });
            }
        }
        $scope.jump = function () {
            if ($scope.num<=0 || $scope.num>b) {
                alert("请输入大于0且不可大于总页数的正整数");
                $scope.num='';
            }else{
                $http({
                    method: 'post',
                    url: '/homePage',
                    data: {
                        pageNum: $scope.num
                    }
                }).then(function (data) {
                    $scope.students = data.data.list;
                    var tt=data.data.list;
                    var color=["active","success","warning","danger","info"];
                    for (var i=0;i<tt.length;i++){
                        var op=tt[i];
                        op.color=color[i%color.length];
                        if(op.sex=='0'){
                            op.sexd="男";
                        }
                        if(op.sex=='1'){
                            op.sexd="女";
                        }
                    }
                    $scope.thepage = data.data.pageNumber;
                    <!--点击按钮把最新的当前页赋值给变量a-->
                    a = data.data.pageNumber;
                    <!--清空输入框-->
                    $scope.num='';
                });
            }
        }
        $scope.delect = function (id) {
            var dd = id;
            console.log(dd);
            layer.msg('您确定要删除吗？', {
                time: 0 //不自动关闭
                ,btn: ['不要多BB', '再想想']
                ,yes: function(index){
                    layer.close(index);
                    $http({
                        method: 'post',
                        url: '/delect',
                        data: {
                            id: id
                        }
                    }).then(function (data) {
                        if (data.data.state == "ok") {
                            location.href = "/home";
                        }
                    })
                }
            });
        }
        $scope.delectlist = function () {
            layer.msg('您确定要删除吗？', {
                time: 0 //不自动关闭
                ,btn: ['确定', '再想想']
                ,yes: function(index){
                    layer.close(index);
                    //获取多选框
                    var obj = document.getElementsByName("uu");
                    //定义数组
                    var checkValue = [];
                    //遍历多选框
                    for (var i = 0; i < obj.length; i++) {
                        //判断多选框是否选中和值是不是为空
                        if (obj[i].checked && obj[i].value != null)
                        //把多选框的值添加到数组中
                            checkValue.push(obj[i].value);
                    }
                    //将数组转为json串
                    var jsonc=angular.toJson(checkValue);
                    //发送请求
                    $http({
                        method:'post',
                        url:'/delectList',
                        data:{
                            idlist:jsonc
                        }
                    }).then(function (data) {
                        if (data.data.state=="ok")
                            location.href="/home";
                    })
                }
            });
        }
        $scope.update=function(id){
        	$state.go('update',{
        		id:id
        	});
        };
    }]);